package com.yytek.nmp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yytek.common.client.base.MqMessage;
import com.yytek.common.client.consts.MqTopic;
import com.yytek.common.client.net.model.IdentityInfo;
import com.yytek.common.client.net.model.SubTopoGetReply;
import com.yytek.common.core.domain.AttributeInfo;
import com.yytek.common.core.domain.CommandRequest;
import com.yytek.common.core.utils.uuid.UUID;
import com.yytek.common.web.domain.ThingModelMessage;
import com.yytek.nmp.domain.IotDeviceTopo;
import com.yytek.nmp.fegin.MqttFegin;
import com.yytek.nmp.mapper.IotDeviceTopoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class IotDeviceTopoServiceImpl {
    @Resource
    private IotDeviceInfoServiceImpl iotDeviceInfoService;
    @Resource
    private IotDeviceTopoMapper iotDeviceTopoMapper;
    @Resource
    private MqttFegin mqttFegin;
    /**
     * 获取网关设备拓扑子设备信息
     * @param mqMsg
     */
    public void getDeviceTopoByMsg(MqMessage mqMsg) {
        String msg = new String(mqMsg.getPayload());
        //解析line中换成接收的参数实体
        ThingModelMessage thingModelMessage= JSON.parseObject
                (msg,ThingModelMessage.class);
        //获取网关设备id
        String iotId = thingModelMessage.getDeviceId();
        //获取拓扑关系
        List<IotDeviceTopo> topoList = iotDeviceTopoMapper.selectIotDeviceTopoByGatewayId(iotId);
        if(CollectionUtil.isNotEmpty(topoList)){
            List<IdentityInfo> deviceTopoMsgs = new ArrayList<>();
            for (int i = 0; i < topoList.size(); i++) {
                String subId =topoList.get(i).getSubId();
                String productKey = iotDeviceInfoService.getDeviceCacheByIotId(subId).getProductKey();
                IdentityInfo topoMsg = new IdentityInfo();
                topoMsg.setDeviceName(subId);
                topoMsg.setProductKey(productKey);
                deviceTopoMsgs.add(topoMsg);
            }
            //网关设备产品key
            String productKey = iotDeviceInfoService.getDeviceCacheByIotId(iotId).getProductKey();
            //网关设备发送到mqtt
            //获取发送信息体
            CommandRequest commandRequest = new CommandRequest();
            //发送信息内容
            Map<String, AttributeInfo> dataInfo = new HashMap<>();
            //发送的topic
            AttributeInfo topic = new AttributeInfo();
            String sendTopic = MqTopic.NetTopic.SUB_TOPO_GET_REPLY_FMT.replace("{productKey}",productKey)
                    .replace("{deviceName}",iotId);
            topic.setValue(sendTopic);
            topic.setType("string");
            dataInfo.put("topic",topic);
            //发送的内容
            AttributeInfo data = new AttributeInfo();
            //发送的返回拓扑关系实体
            SubTopoGetReply subTopoGetReply = new SubTopoGetReply();
            subTopoGetReply.setCode(200);
            subTopoGetReply.setId(UUID.fastUUID().toString());
            subTopoGetReply.setData(deviceTopoMsgs);
            //转换成json放入data
            data.setValue(JSONObject.toJSONString(subTopoGetReply));
            data.setType("string");
            dataInfo.put("data",data);
            //放入写入消息体
            commandRequest.setDeviceId(iotId);
            commandRequest.setDataInfo(dataInfo);
            //写入网关设备统一网关
            mqttFegin.write(commandRequest);
        }
    }
}
